<template>
  <div>
    <el-form ref="adSRef" :inline="true" :model="adCl.adS">
      <el-form-item label="名称：" prop="name">
        <el-input v-model="adCl.adS.name" placeholder="名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="位置" prop="position">
        <el-select v-model="adCl.adS.position" placeholder="位置">
          <el-option v-for="item in adCl.positionList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(adSRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加推广
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="adCl.adTable" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (adCl.adS.page - 1) * adCl.adS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="名称" prop="name" width="180" />
      <el-table-column align="center" header-align="center" label="类型" prop="code" />
      <el-table-column align="center" header-align="center" label="code" prop="position" width="180" />
      <el-table-column align="center" header-align="center" label="位置" prop="position" width="180">
        <template #default="scope">
          <span v-if="scope.row.position === 'WECHAT_HOME_POLL'">微信首页轮询</span>
          <span v-if="scope.row.position === 'WECHAT_HOME_WAIST'">微信首页腰线</span>
          <span v-if="scope.row.position === 'WECHAT_SHOP_CART'">微信购物车</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="url" prop="connectUrl" width="180" />
      <el-table-column align="center" header-align="center" label="状态" prop="avail" width="180">
        <template #default="scope">
          <el-switch v-model="scope.row.avail" :active-value="true" :inactive-value="false" active-text="是" inactive-text="否" inline-prompt @change="chengWork(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="220">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="toEdit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteAds(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="adCl.openLabel" title="添加推广" destroy-on-close @close="resetDialog(adRef)">
      <el-form ref="adRef" :model="adCl.adEdit" :rules="adCl.adRule">
        <el-form-item label="父推广" prop="type">
          <el-select v-model="adCl.adEdit.pid" placeholder="父推广">
            <el-option v-for="item in adCl.adParentList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="adCl.adEdit.name" placeholder="名称" type="text"></el-input>
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input-number v-model="adCl.adEdit.code" :max="10000" :min="0" :step="1" />
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-select v-model="adCl.adEdit.position" placeholder="位置">
            <el-option v-for="item in adCl.positionList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户端类型" prop="clientType">
          <el-select v-model="adCl.adEdit.clientType" placeholder="客户端类型">
            <el-option v-for="item in adCl.clientTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="链接地址" prop="connectUrl">
          <el-input v-model="adCl.adEdit.connectUrl" placeholder="链接地址" type="text"></el-input>
        </el-form-item>
        <el-form-item label="推荐首图" prop="imageUrl">
          <ImgUpload v-model:file_url="adCl.adEdit.imageUrl" :file_max="1" />
        </el-form-item>
        <el-form-item label="备注" prop="describes">
          <Editor v-model:editorValue="adCl.adEdit.describes"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editAd">保存 </el-button>
          <el-button @click="adCl.openLabel = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <Page :pageData="adCl.adS" @selectPage="search"></Page>
</template>
<script lang="ts" setup>
import { addAd, updateAd, pageAd, deleteAd, detailAd, getAdSelect } from '@/api/market'
import { selectDictValue } from '@/api/system'
import Editor from '@/components/MyEditor.vue'
import { adClass } from '@/page/types/market/ad'
import ImgUpload from '@/components/ImgsUpload.vue'
import { ElMessage, FormInstance } from 'element-plus/es'
import Page from '@/components/Page.vue'
const adSRef = ref()
const adRef = ref()
const adCl = reactive(new adClass())
const search = () => {
  pageAd(adCl.adS).then(res => {
    adCl.adTable = res.data.records
    adCl.adS.page = res.data.current
    adCl.adS.size = res.data.size
    adCl.adS.total = res.data.total
  })
}
const resetS = form => {
  if (!form) return
  form.resetFields()
  search()
}
const opendialog = () => {
  getAdSelect(true, '').then(res => {
    adCl.openLabel = true
    adCl.adParentList = res.data
  })
}
const toEdit = id => {
  detailAd(id).then(res => {
    adCl.adEdit = res.data
    adCl.openLabel = true
  })
}
const resetDialog = (rest: any) => {
  if (!rest) return
  rest.resetFields()
  adCl.adEdit = {}
}
const deleteAds = id => {
  deleteAd(id).then(res => {
    search()
    ElMessage({
      message: '删除推广信息成功',
      type: 'success'
    })
  })
}
const editAd = () => {
  if (adCl.adEdit.id) {
    adCl.adEdit.clientType = adCl.adEdit.name
    updateAd(adCl.adEdit).then(res => {
      search()
      ElMessage({
        message: '更新推广信息成功',
        type: 'success'
      })
    })
  } else {
    addAd(adCl.adEdit).then(res => {
      search()
      ElMessage({
        message: '添加推广信息成功',
        type: 'success'
      })
    })
  }
  adCl.openLabel = false
}
const chengWork = (row: any) => {
  const data = {
    id: row.id,
    avail: row.avail
  }
  updateAd(data).then(res => {
    search()
    ElMessage({
      message: row.avail ? '开启推广成功' : '禁用推广成功',
      type: 'success'
    })
  })
}
const getPosition = () => {
  selectDictValue('ADPOSITION').then(res => {
    adCl.positionList = res.data
  })
}
onMounted(() => {
  search()
  getPosition()
})
</script>
<style lang="scss" scoped>
.el-select {
  width: 200px;
}
</style>
